package com.sphd.emanu.activity.discountcoupon;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.github.yoojia.inputs.AndroidNextInputs;
import com.github.yoojia.inputs.StaticScheme;
import com.rengwuxian.materialedittext.MaterialEditText;
import com.sphd.emanu.R;
import com.sphd.emanu.config.Constant;
import com.sphd.emanu.mvp.MVPBaseActivity;
import com.sphd.emanu.permission.Moudle;
import com.sphd.emanu.pojo.login.User;
import com.sphd.emanu.pojo.market.Coupon;
import com.sphd.emanu.pojo.market.Rules;
import com.sphd.emanu.utils.PermissionConfig;
import com.wzutils.BigDisMailUtils;
import com.wzutils.L;

import java.math.BigDecimal;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 电子优惠券界面
 *
 * @Author Hu
 * @Time 2017/5/12 -- 13:57.
 * @Email 87683202@qq.com
 */
public class DiscountcouponActivity extends MVPBaseActivity<DiscountcouponContract.View, DiscountcouponPresenter> implements DiscountcouponContract.View {

    @BindView(R.id.btn_save)
    TextView btnSave;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.discount_coupon_switch)
    Switch discountCouponSwitch;
    @BindView(R.id.layout_discount_coupon)
    LinearLayout layoutDiscountCoupon;
    @BindView(R.id.coup_ditail)
    MaterialEditText coupDitail;
    @BindView(R.id.coup_money)
    MaterialEditText coupMoney;
    @BindView(R.id.coup_use_money)
    MaterialEditText coupUseMoney;
    @BindView(R.id.coup_use_days)
    MaterialEditText coupUseDays;
    @BindView(R.id.discount_coupon_switch_give)
    Switch discountCouponSwitchGive;
    Intent intent;
    Bundle bundle;
    Rules rules;
    Coupon coupon;
    AndroidNextInputs inputs;
    User user;
    List<Moudle> moudles;
    boolean isCanDo=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discountcoupon);
        ButterKnife.bind(this);
        initView();
        initEvent();
    }

    private void initEvent() {
        toolbar.setNavigationOnClickListener(v -> finish());
        discountCouponSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    layoutDiscountCoupon.setVisibility(View.VISIBLE);
                } else {
                    layoutDiscountCoupon.setVisibility(View.GONE);
                }
            }
        });
    }

    private void initView() {
        setTitleBar(toolbar, "电子优惠券");
        inputs = new AndroidNextInputs();
        inputs.add(coupDitail, StaticScheme.Numeric().msgOnFail("请输入整数"), StaticScheme.NotBlank().msgOnFail("该项不能为空"));
        inputs.add(coupMoney, StaticScheme.Numeric().msgOnFail("请输入整数"), StaticScheme.NotBlank().msgOnFail("该项不能为空"));
        inputs.add(coupUseMoney, StaticScheme.Numeric().msgOnFail("请输入整数"), StaticScheme.NotBlank().msgOnFail("该项不能为空"));
        inputs.add(coupUseDays, StaticScheme.Numeric().msgOnFail("请输入整数"), StaticScheme.NotBlank().msgOnFail("该项不能为空"));
        intent = getIntent();
        bundle = intent.getExtras();
        rules = (Rules) bundle.getSerializable(Constant.RULES);
        user = (User) bundle.getSerializable(Constant.USER_ENTITY);
        moudles= (List<Moudle>) bundle.getSerializable(Constant.PERMISSION_LIST);
        isCanDo=isCanDo(moudles, PermissionConfig.MENU_MAKETING_4);
        if (!isCanDo){
            btnSave.setVisibility(View.GONE);
        }
        coupon = rules.getCoupon();
        if (coupon == null) {
            discountCouponSwitch.setChecked(false);
            discountCouponSwitchGive.setChecked(false);
        } else {
            discountCouponSwitch.setChecked(coupon.getEnable());
            discountCouponSwitchGive.setChecked(coupon.getIsLoop());
            coupDitail.setText(BigDisMailUtils.turnTrim(coupon.getThreshold(), Constant.POINT));
            coupMoney.setText(String.valueOf(coupon.getCoupon()));
            coupUseMoney.setText(String.valueOf(coupon.getConditions()));
            coupUseDays.setText(String.valueOf(coupon.getExpireDays()));
            discountCouponSwitchGive.setChecked(coupon.getIsLoop());
        }

        if (discountCouponSwitch.isChecked()) {
            layoutDiscountCoupon.setVisibility(View.VISIBLE);
        } else {
            layoutDiscountCoupon.setVisibility(View.GONE);
        }
    }

    @OnClick(R.id.btn_save)
    public void onViewClicked() {
//        mPresenter.updateCoupon(coupon == null ? null : coupon.getId(), true, viewGetText(coupDitail), viewGetText(coupMoney),
//                        viewGetText(coupUseMoney), 30,
//                        discountCouponSwitchGive.isChecked(), rules.getConfig().getId(), true, user.getOid());

        if (discountCouponSwitch.isChecked()) {
            if (inputs.test()) {
                mPresenter.updateCoupon(coupon == null ? null : coupon.getId(), true, viewGetText(coupDitail), viewGetText(coupMoney),
                        viewGetText(coupUseMoney), Integer.valueOf(coupUseDays.getText().toString().trim()),
                        discountCouponSwitchGive.isChecked(), rules.getConfig().getId(), true, user.getOid());
            } else {
                toast("请按提示要求填写");
            }
        } else {
            mPresenter.updateCoupon(coupon == null ? null : coupon.getId(), false, null, null, null, null, null, rules.getConfig().getId(), false, user.getOid());
        }
    }

    @Override
    public void upRes(int res) {
        if (res == 1) {
            toast("修改成功");
            setResult(Constant.REQUEST_CODE);
            finish();
        } else {
            toast("修改失败");
        }
    }

    private BigDecimal viewGetText(MaterialEditText materialEditText) {
        return BigDisMailUtils.turn(materialEditText.getText().toString().trim(), Constant.POINT);
    }
}
